package com.ssh.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import com.ssh.bean.User_SunShuHao;
import com.ssh.dao.UserDAO_SunShuHao;
/**
 * 
 * @author 孙树昊
 *
 */
@Repository
public class UserDAOImpl_SunShuHao implements UserDAO_SunShuHao {
	@Resource
	private SessionFactory sessionFactory;
	private Transaction tr;
	private Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	
	public void deleteUser(User_SunShuHao user) {
		this.getSession().delete(user);
	}

	public void insertUser(User_SunShuHao user) throws Exception{
		try {
			getSession().save(user);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("插入失败");
		}
	}

	public User_SunShuHao queryUser(String name, String password) {
		List<User_SunShuHao> users = getSession().createQuery("from User u where u.username=? and u.password=?")
								.setParameter(0, name)
								.setParameter(1, password)
								.list();
		if(users.size()!=0){
			return users.get(0);
		}
		return null;
	}

	public void updateUser(User_SunShuHao user) {
		getSession().update(user);
	}

}
